<?php
$this->pageHeader = t('PAGE_TITLE', 'profile');
$this->pageTitle = t('PAGE_TITLE', 'profile');
$logedInUser = User::model()->findByPk(Yii::app()->user->id);
$toolbar = new LGToolbar();
if ($logedInUser->isAdmin() || $logedInUser->isSuperAdmin()) {
    $toolbar->buttonCustom('Thay đổi trạng thái hồ sơ', '', 'button', array('data-dropdown' => '#dropdown-1'));
    $toolbar->buttonExport(t('BTN_EXPORT_ALL', 'profile'), t('BTN_EXPORT_ALL', 'profile'), 'buttonExportAllToExcel', '', '/profile/exportall');
}
if (!$logedInUser->isSubmitter()) {
    $toolbar->buttonExport(t('BTN_EXPORT', 'profile'), t('BTN_EXPORT', 'profile'), 'buttonExportToExcel', '', '/profile/export');
}
if ($logedInUser->isSubmitter() || $logedInUser->isAdmin() || $logedInUser->isSuperAdmin()) {
    $toolbar->buttonNewPage(t('PAGE_TITLE_ADDNEW', 'profile'), '', app()->controller->id . '/create');
}
$toolbar->buttonDelete();
$toolbar->buttonRefresh();
$this->pageToolbar = $toolbar->mergeToolbar();
?>   
<!-- search-form -->                                
<div class="search-form"> 														
    <?php
    $this->renderPartial('_search', array(
        'model' => $model,
    ));
    ?>
</div>    
<?php
$this->widget('GridView', array(
    'id' => 'DataGrid',
    'dataProvider' => $model->search(),
    'itemsCssClass' => 'grid',
    'summaryText' => LGHelpers::getSummary(),
    'columns' => array(
        array(
            'header' => '#',
            'class' => 'MyCheckBoxColumn',
            'selectableRows' => '2',
            'disabledException' => array('status' => array(ProfileStatus::STATUS_NEW, ProfileStatus::STATUS_REJECTED), 'checkRole' => ($logedInUser->isSubmitter())),
            'verifyField' => '$data->status',
            'htmlOptions' => array('width' => '10px', 'align' => 'center'),
            'id' => 'chkId',
        ),
        array(
            'name' => 'full_name',
            'value' => 'Html::linkDetailProfile($data)',
            'htmlOptions' => array('width' => '140px'),
            'headerHtmlOptions' => array('align' => 'left'),
            'type' => 'raw',
        ),
        array(
            'name' => 'birth_place',
            'value' => 'CHtml::encode($data->province->province_name)',
            'htmlOptions' => array('width' => '80px', 'align' => 'center'),
        ),
        array(
            'name' => 'age',
            'value' => 'CHtml::encode($data->age)',
            'headerHtmlOptions' => array('align' => 'center'),
            'htmlOptions' => array('width' => '30px', 'align' => 'center'),
            'type' => 'raw',
        ),
        array(
            'name' => 'sex',
            'value' => '(CHtml::encode($data->sex)==1)?t("DROP_DOWN_ITEM_MALE", "profile"):t("DROP_DOWN_ITEM_FEMALE", "profile")',
            'htmlOptions' => array('width' => '40px', 'align' => 'center'),
        ),
        array(
            'name' => 'status',
            'value' => 't("STATUS_" . $data->status, "profile")',
            'htmlOptions' => array('width' => '80px', 'align' => 'center'),
            'type' => 'raw',
        ),
        array(
            'header' => t('LBL_NAME', 'union'),
            'value' => '$data->advanceProfile->hiringCompany->union->name',
            'htmlOptions' => array('width' => '80px', 'align' => 'center'),
        ),
        array(
            'header' => t('LBL_HIRING_COMPANY', 'hiringCompany'),
            'value' => '$data->advanceProfile->hiringCompany->name',
            'htmlOptions' => array('width' => '100px', 'align' => 'center'),
        ),
        array(
            'header' => t('LBL_INTRODUCING_COMPANY', 'introducingCompany'),
            'value' => '$data->advanceProfile->introducingCompany->name',
            'htmlOptions' => array('width' => '100px', 'align' => 'center'),
        ),
        array(
            'header' => t('LBL_GO_DATE', 'advanceProfile'),
            'value' => '$data->advanceProfile->go_date',
            'htmlOptions' => array('width' => '80px', 'align' => 'center'),
        ),
        array(
            'header' => t('LBL_ORDER', 'advanceProfile'),
            'value' => '$data->advanceProfile->order',
            'htmlOptions' => array('width' => '80px', 'align' => 'center'),
        ),
        array(
            'header' => t('LBL_ACTION', 'profile'),
            'class' => 'ProfileButtonColumn',
            'htmlOptions' => array('width' => '80px', 'align' => 'center'),
            'buttons' => array(
                'action' => array('current_user' => $logedInUser),
            )
        ),
    ),
));
?>
<?php
if ($logedInUser->isEditor() || $logedInUser->isAdmin() || $logedInUser->isSuperAdmin() || $logedInUser->isManager()) {
    echo '
<div id="search_by_code" style="display:none">
	<div style="float: left"><table>
		<tr>
			<td>
			<td>'
    . CHtml::textField('buttonSearchByCode', '', array('id' => 'textfield_code_search', 'style' => 'width:60px; padding:6px 6px 8px 6px; border-color: #ddd; border-right:none; border-radius: 0', 'placeholder' => t('MSG_ADVANCE_PROFILE_SEARCH_TEXTBOX', 'messages')))
    . CHtml::button(t('BTN_SEARCH', 'profile'), array('id' => 'buttonSearchByCode', 'class' => 'button filter', 'type' => 'button', 'style' => 'border-radius: 0 2px 2px 0', 'onclick' => 'searchByCode()'))
    . CHtml::label('', false, array('id' => 'error_search_by_code', 'class' => 'errorMessage', 'style' => 'color: red'))
    . '</td>
		</tr>
	</table></div>
</div>';
}
?>

<?php if ($logedInUser->isAdmin() || $logedInUser->isSuperAdmin()): ?>
    <div id="dropdown-1" class="dropdown dropdown-tip">
        <ul class="dropdown-menu">
            <li><a data="PASS">Đậu phỏng vấn</a></li>
            <li><a data="STAY">Lưu trú</a></li>
            <li><a data="FLY">Bay</a></li>
            <li><a data="COMPLETE">Hoàn thành hợp đồng</a></li>
            <li><a data="OUT">Rút hồ sơ</a></li>		
        </ul>
    </div>

    <link type="text/css" rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl;
    ; ?>/js/dropdown/jquery.dropdown.css" />
    <script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/js/dropdown/jquery.dropdown.js"></script>
    <script type="text/javascript">
        $('#dropdown-1 ul li').each(function () {
            $(this).find('a').bind('click', function () {
                $('#dropdown-1').hide();
                $('button[data-dropdown="#dropdown-1"]').removeClass('dropdown-open');
                var _ids = $.fn.yiiGridView.getChecked('DataGrid', 'chkId');
                if (_ids.length == 0) {
                    LGCMS.kAlert('Thay đổi trạng thái hồ sơ', 'Vui lòng chọn ít nhất một hồ sơ trước khi thực hiện lên này', 450, 150);
                } else {
                    window.location.href = '<?php echo url('profile/status') ?>?s=' + $(this).attr('data') + '&id=' + $.fn.yiiGridView.getChecked('DataGrid', 'chkId');
                }
                return false;
            });
        });
    </script>
<?php endif; ?>

<script type="text/javascript">
    $("#buttonExportToExcel").click(function ()
    {
        var _ids = $.fn.yiiGridView.getChecked('DataGrid', 'chkId');
        if (_ids.length == 0) {
            $("#buttonExportToExcel").removeAttr('onclick');
            LGCMS.kAlert('Kết xuất dữ liệu', 'Vui lòng chọn ít nhất một hồ sơ trước khi thực hiện lên này', 450, 150);
        } else {
            window.location.href = $('#buttonExportToExcel').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid', 'chkId');
        }
    });

    $("#buttonExportAllToExcel").click(function ()
    {
        var _ids = $.fn.yiiGridView.getChecked('DataGrid', 'chkId');
        if (_ids.length == 0) {
            $("#buttonExportAllToExcel").removeAttr('onclick');
            LGCMS.kAlert('Kết xuất dữ liệu', 'Vui lòng chọn ít nhất một hồ sơ trước khi thực hiện lên này', 450, 150);
        } else {
            window.location.href = $('#buttonExportAllToExcel').attr('action') + '?id=' + $.fn.yiiGridView.getChecked('DataGrid', 'chkId');
        }
    });

    function searchByCode()
    {
        var _code = $('#textfield_code_search').val();
        if (_code == '')
        {
            LGCMS.kAlert('Thông báo', 'Vui lòng nhập mã hồ sơ trước khi tìm. Bạn có muốn tìm lại lần nữa? <p style="text-align:center"><br /><input type="button" class="button" value="Tìm lại" onclick="focusCode()"> <input type="button" class="button" value="Đóng" onclick="cancelSearch();"></p>', 480, 150);
        }
        else
        {
            $.ajax({
                url: '<?php echo url('profile/getprofilebycode'); ?>',
                type: "get",
                dataType: "json",
                data: {code: _code},
                cache: false,
                success: function (response) {
                    if (response.status == 'success') {
                        window.location.href = response.redirectUrl;
                    } else {
                        LGCMS.kAlert('Mã hồ sơ không tồn tại', 'Vui lòng nhập mã khác. Bạn có muốn tìm lại lần nữa? <p style="text-align:center"><br /><input type="button" class="button" value="Tìm lại" onclick="focusCode()"> <input type="button" class="button" value="Đóng" onclick="cancelSearch();"></p>', 400, 150);
                    }
                }
            });
        }
    }
    ;

    function focusCode()
    {
        LGCMS.kAlertClose();
        $('#textfield_code_search').focus().select();
    }
    ;

    function cancelSearch()
    {
        LGCMS.kAlertClose();
        $('#textfield_code_search').val('');
    }
    ;

    $(document).ready(function () {
        $('.bar-control').prepend($('#search_by_code').html());
        $('#search_by_code').html('');

        $('#textfield_code_search').keyup(function (e) {
            /*
             * Delay the enter key form submit till after the hidden
             * input is updated.
             */

            // No need to do anything if it's not the enter key
            // Also only e.which is needed as this is the jQuery event object.
            if (e.which != 13) {
                return;
            }

            // Prevent form submit
            e.preventDefault();

            // Trigger the blur event.
            this.blur();

            // Submit the form.
            searchByCode();
        });

    });
</script>